<template>
  <div class="container">
    <a-button type="primary" @click="addNewItem" style="width: 150px;margin-bottom: 20px">
      <template #icon><plus-outlined /></template>
      新增配置
    </a-button>
    <div class="scroll-wrapper">
      <div v-for="(item, index) in configItems" :key="index" class="config-item">
        <a-space>
          <a-input
              v-model:value="item.name"
              placeholder="配置名称"
              size="small"
              style="width: 200px"
              @change="saveConfig"
          />

          <div class="tags-editor">
            <a-tag
                v-for="(tag, tagIndex) in item.tags"
                :key="tagIndex"
                closable
                @close="() => removeTag(index, tagIndex)"
            >
              {{ tag }}
            </a-tag>
            <a-input
                v-if="item.inputVisible"
                ref="inputRef"
                v-model:value="item.inputValue"
                size="small"
                style="width: 100px"
                @blur="handleInputConfirm(index)"
                @pressEnter="handleInputConfirm(index)"
            />
            <a-button
                v-else
                size="small"
                type="dashed"
                @click="showInput(index)"
            >
              <plus-outlined />
              添加标签
            </a-button>
          </div>

          <a-button size="small" danger @click="removeItem(index)">
            <template #icon><delete-outlined /></template>
          </a-button>
        </a-space>
      </div>
    </div>

  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import { LoadConfig, SaveConfig } from '../../../wailsjs/go/main/App'
import { message } from 'ant-design-vue'
import { PlusOutlined, DeleteOutlined } from '@ant-design/icons-vue'

const configItems = ref([])
const inputRef = ref()

// 加载配置
const loadConfig = async () => {
  try {
    const items = await LoadConfig()
    configItems.value = items.map(item => ({
      ...item,
      inputVisible: false,
      inputValue: ''
    }))
  } catch (err) {
    message.error('配置加载失败: ' + err.message)
  }
}

// 保存配置
const saveConfig = async () => {
  try {
    const items = configItems.value.map(item => ({
      name: item.name,
      tags: item.tags
    }))
    await SaveConfig(items)
    message.success('配置保存成功')
  } catch (err) {
    message.error('配置保存失败: ' + err.message)
  }
}

// 新增配置项
const addNewItem = () => {
  configItems.value.push({
    name: '',
    tags: [],
    inputVisible: false,
    inputValue: ''
  })
}

// 删除配置项
const removeItem = (index) => {
  configItems.value.splice(index, 1)
  saveConfig()
}

// 标签处理逻辑
const showInput = (index) => {
  configItems.value[index].inputVisible = true
  nextTick(() => {
    inputRef.value[index].focus()
  })
}

const handleInputConfirm = (index) => {
  const newTag = configItems.value[index].inputValue.trim()
  if (newTag) {
    configItems.value[index].tags.push(newTag)
    saveConfig()
  }
  configItems.value[index].inputVisible = false
  configItems.value[index].inputValue = ''
}

const removeTag = (itemIndex, tagIndex) => {
  configItems.value[itemIndex].tags.splice(tagIndex, 1)
  saveConfig()
}

// 初始化加载
onMounted(() => {
  loadConfig()
})
</script>

<style>
.config-item {
  margin-bottom: 16px;
  padding: 3px;
  border: 1px solid #f0f0f0;
  border-radius: 4px;
}

.tags-editor {
  min-width: 400px;
  padding: 8px;
}

.tags-editor .ant-tag {
  margin-bottom: 4px;
}
/* 容器高度设置 */
.container {
  height: 100vh; /* 全屏高度 */
  padding: 20px;
  display: flex;
  flex-direction: column;
}

/* 滚动区域样式 */
.scroll-wrapper {
  flex: 1;
  overflow-y: auto; /* 垂直滚动 */
  padding-right: 8px; /* 防止滚动条遮挡内容 */

  /* 自定义滚动条样式 */
  &::-webkit-scrollbar {
    width: 6px;
  }
  &::-webkit-scrollbar-track {
    background: #f1f1f1;
  }
  &::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
  }
}

/* 单个配置项样式 */
.config-item {
  margin-bottom: 16px;
  padding: 3px;
  border: 1px solid #f0f0f0;
  border-radius: 4px;
  background: #fff;
}

/* 标签编辑器高度限制 */
.tags-editor {
  min-width: 400px;
  max-height: 120px; /* 限制标签区域高度 */
  overflow-y: auto;  /* 标签过多时内部滚动 */
}
</style>